package com.opi.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.opi.util.CommUtil;

public class ServerAction extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(ServerAction.class);

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	    String method = "";
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		method = request.getParameter("m");
		
		if(method!=null){
			String command = "";
			if(method.equals("tomcatStop")){
				logger.info("请求：停止tomcat");
				command = "service tomcat stop";
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				String resp = CommUtil.sendCmd(command);
				if(!resp.equals("error")){
					out.write("ok");
				}else{
					out.write("error");
				}
				
			}else if(method.equals("tomcatResart")){
				
				logger.info("请求：重启tomcat");
				command = "service tomcat restart";
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					logger.error(e);
				}
				String resp = CommUtil.sendCmd(command);
				if(!resp.equals("error")){
					out.write("ok");
				}else{
					out.write("error");
				}
				
			}else{
				out.write("error");
			}
		}else{
			out.write("error");
		}
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
